package fr.nts.framework.database.jpa.model.common;

import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import fr.nts.framework.database.jpa.enumeration.NTSDroit;
import fr.nts.framework.database.jpa.model.AJPAModel;

/**
 * Represente un menu de NTS pour une application NTS.
 * 
 * @author sco
 * @version 1.0.0
 */
@Entity
@Table(name = "simple_menu")
public class SimpleMenu extends AJPAModel<Long>{
    
    private static final long serialVersionUID = 1L;
    
    private static final Logger log = LoggerFactory.getLogger(SimpleMenu.class);
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, length = 10, unique = true)
    private Long id;
    
    /**
     * Le titre du menu
     */
    @Column(name = "title", nullable = false, length = 255, unique = true)
    private String title;
    
    /**
     * L'url associé au menu
     */
    @Column(name = "url", nullable = true, length = 255)
    private String url;
    
    /**
     * Le menu parent si existant
     */
    @ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.REMOVE})
    @JoinColumn(name = "parent_menu", nullable = true)
    private SimpleMenu parentMenu;
    
    /**
     * La liste des sous menus
     */
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "parentMenu", cascade = {CascadeType.ALL})
    private Set<SimpleMenu> subMenus = Sets.newHashSet();
    
    /**
     * La position
     */
    @Order
    @Column(name = "position", nullable = false, length = 2)
    private int position = 1;
    
    /**
     * A cacher
     */
    @Order
    @Column(name = "hidden", nullable = false, length = 1)
    private boolean hidden = false;
    
    /**
     * Les droits associés au menu
     */
    @ElementCollection(fetch = FetchType.EAGER)  
    @CollectionTable(name = "simple_menu_rights", joinColumns = {@JoinColumn(name = "menu_id")})  
    @Column(name = "right")  
    @Enumerated(EnumType.STRING)
    private List<NTSDroit> droits = Lists.newArrayList();  
    
    
    /*
     * (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString(){
    	if(getLog().isDebugEnabled()){
	    	return new ToStringBuilder(this).append("id", id)
	    	                                .append("title", title)
	    	                                .append("url", url)
	    	                                .append("parentMenu", parentMenu == null? null: parentMenu.toString())
	    	                                .append("Nb subMenu", subMenus.size())
	    	                                .append("position", position)
	    	                                .append("hidden", hidden)
	    	                                .append("Nb droit", droits.size())
	                                        .toString();
    	}else{
    		return super.toString();
    	}
    }


    /*
     * (non-Javadoc)
     * @see fr.nts.framework.database.jpa.model.AJPAObject#getLog()
     */
    @Override
    protected Logger getLog(){
    	return log;
    }


    // GETTER / SETTER
	public Long getId(){
		return id;
	}

	public void setId(Long id){
		this.id = id;
	}

	public String getTitle(){
		return title;
	}

	public void setTitle(String title){
		this.title = title;
	}

	public String getUrl(){
		return url;
	}

	public void setUrl(String url){
		this.url = url;
	}

	public SimpleMenu getParentMenu(){
		return parentMenu;
	}

	public void setParentMenu(SimpleMenu parentMenu){
		this.parentMenu = parentMenu;
	}

	public Set<SimpleMenu> getSubMenus(){
		return subMenus;
	}

	public void setSubMenus(Set<SimpleMenu> subMenus){
		this.subMenus = subMenus;
	}

	public int getPosition(){
		return position;
	}

	public void setPosition(int position){
		this.position = position;
	}

	public boolean isHidden(){
		return hidden;
	}

	public void setHidden(boolean hidden){
		this.hidden = hidden;
	}

	public List<NTSDroit> getDroits(){
		return droits;
	}

	public void setDroits(List<NTSDroit> droits){
		this.droits = droits;
	}
}